package com.fsy.beanandsql.beantosql;

import com.fsy.beanandsql.beantosql.Bean.TestBean;
import com.fsy.beanandsql.beantosql.Bean.TestChildBean;
import com.fsy.beanandsql.beantosql.Bean.TestChildsBean;

import javax.persistence.PrimaryKeyJoinColumn;

/**
 * Created by shuangyufeng on 2017/5/5.
 */
public class TestGetPrimkeyName {

    public static void main(String[] args){

        //普通类获取类名称
        TestBean testBean = new TestBean();
        getPrimkeyName(testBean.getClass());

        System.out.println("<<<<<<<<===========分割线======================>>>>>>>");
        //继承类获取父类的名称
        TestChildBean testChildBean = new TestChildBean();
        getPrimkeyName(testChildBean.getClass());

        System.out.println("<<<<<<<<===========分割线======================>>>>>>>");
        //继承类获取父类的名称 再加一层试试
        TestChildsBean testChildsBean = new TestChildsBean();
        getPrimkeyName(testChildsBean.getClass());
    }


    private static void getPrimkeyName(Class<?> objClass){
        PrimaryKeyJoinColumn primaryKeyJoinColumn = objClass.getAnnotation(PrimaryKeyJoinColumn.class);
        if(primaryKeyJoinColumn == null && objClass.getSuperclass() != null){
            getPrimkeyName(objClass.getSuperclass());
            return;
        }
        System.out.println("primaryKeyJoinColumn name == " + primaryKeyJoinColumn.name());
    }

}
